/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.databox.monitorelement;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import twaver.DataBoxEvent;
import twaver.DataBoxListener;
import twaver.DataBoxSelectionAdapter;
import twaver.DataBoxSelectionEvent;
import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.combobox.TComboBox;
import twaver.list.TList;
import demo.DemoPane;
import demo.DemoUtil;

public class MonitorElementDemo extends DemoPane {
	
	private int sequence = 0;
	private TDataBox box = new TDataBox();	
	protected static JTextArea track = DemoUtil.createColorTextArea();

	public MonitorElementDemo(){
        this.initBoxListener();
		
		for(sequence=0; sequence<10; sequence++){
			Element element = new Node();
			element.setName("ele" + sequence);
			element.setIcon("/demo/list/choose/online.gif");
			box.addElement(element);
		}
		JPanel panel=new JPanel(new BorderLayout(10,10));
		panel.add(new TList(box));
		panel.add(new TComboBox(box),BorderLayout.NORTH);
        JSplitPane topSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(panel), new JScrollPane(createControlPanel()));
		topSplit.setDividerLocation(400);

		JSplitPane mainSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topSplit, new JScrollPane(track));
		mainSplit.setDividerLocation(300);
		
        this.add(mainSplit);
	}
	
	private void initBoxListener(){
		box.addDataBoxListener(new DataBoxListener(){
			public void elementAdded(DataBoxEvent e) {
				track.append(e.getElement().getName() + " is added.");
			}

			public void elementRemoved(DataBoxEvent e) {
				track.append(e.getElement().getName() + " is removed.");
			}

			public void elementsCleared(DataBoxEvent e) {
				track.append("box is cleared.");
			}
		});
		box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionAdapter(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				List list = box.getSelectionModel().getAllSelectedElement();
				StringBuffer sb = new StringBuffer();
				for(int i=0; i<list.size(); i++){
					Element element = (Element)list.get(i);
					sb.append(element.getName() + " ");
				}
				track.append("selection >> " + sb.toString());
			}
		});
	}
	
	private JPanel createControlPanel(){
		JPanel controlPanel = TWaverUtil.createVerticalPanel(7);
		
		JButton button = new JButton("Add Element");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Element element = new Node();
				element.setName("ele" + sequence++);
				element.setIcon("/demo/list/choose/online.gif");
				box.addElement(element);
			}
		});
		button = new JButton("Remove Element");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.removeSelectedElements();
			}
		});
		button = new JButton("Clear Box");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.clear();
			}
		});
		button = new JButton("Select All");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.selectAll();
			}
		});
		button = new JButton("Select None");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.getSelectionModel().clearSelection();
			}
		});
		button = new JButton("Move Top");
		controlPanel.add(button);
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				box.moveSelectionToTop();
			}
		});
		return controlPanel;
	}
	
}